const {forumList} = require("../../api/index")
Page({

    data: {
        list: [],
        currentList: [],
        name: "",
        pageSize:10,
        pageNum: 1,
        total: 0,
        flag: true, // 防抖开关 防止用户不停的下拉
    },
    async onLoad(options) {
        this.getData()

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    async onShow() {

},
    async getData() {
        const obj = {
            parentid: 0,
            isdone: '开放',
            sort: 'isTop,topTime,addtime',
            order: 'desc,desc,desc',
            page: this.data.pageNum,
            limit: this.data.pageSize,

        }
        let resList  = await forumList(obj);
        if(resList.code==0){
            let mylist = this.data.list;
            // 新旧数据合并到一起
            let resultdata;
            if (this.data.pageNum == 1) {
                mylist = []
            }
            if(resList.data.list.length>0){
                resultdata = mylist.concat(resList.data.list)
            }
            if (mylist.length < resList.data.total ){
                this.setData({
                    pageNum:obj.page+ 1,
                    flag:true
                })
            } else {
                this.setData({
                    flag:false
                })
            }
            this.setData({
                list: resultdata,
                currentList:resultdata,
                name: ""
            })
        }

    },
    searhandler() {
        const result = this.data.list.filter((item, index) => {
            if (item.title.includes(this.data.name)) {
                return item
            }

        })
        this.setData({
            currentList: result
        })
    },


    async todetail(e) {
        const id = e.currentTarget.dataset.id;
        wx.setStorageSync('parentId', id)
// 将数组转换为字符串
        wx.navigateTo({
            url: '/pages/forum/forum-detail',
        })
    },
    /**
     * 生命周期函数--监听页面显示
     */


    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {
    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {


        if (this.data.flag) {
            this.setData({
                flag: false
            })
            this.getData(); // 疯狂的请求的方法
        }

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})